/**
 * Choicesoft.com.cn Inc.
 * Copyright (c) 2004-2018 All Rights Reserved.
 */
package com.example.demo.common;


import java.io.Serializable;

/**
 * @auther: fh
 * @date 2019/4/30 11:25
 * @des 返回信息格式
 */
public class MessageBody implements Serializable {
    private static final long serialVersionUID = 1L;
    private int code;
    private String msg;
    private Object extInfo;
    private Object data;
    private boolean success;

    @Override
    public String toString() {
        return "MessageBody{" +
                "code:" + code +
                ", msg:'" + msg + '\'' +
                ", extInfo:" + extInfo +
                ", data:" + data +
                ", success:" + success +
                '}';
    }

    public MessageBody() {
        setSuccess(true);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public Object getExtInfo() {
        return extInfo;
    }

    public void setExtInfo(Object extInfo) {
        this.extInfo = extInfo;
    }

    public static MessageBody getMessageBody(boolean success, Object data) {
        MessageBody body = new MessageBody();
        setVal(success, body);
        body.setData(data);
        return body;
    }

    public static MessageBody getMessageBody(boolean success) {
        MessageBody body = new MessageBody();
        setVal(success, body);
        return body;
    }

    public static MessageBody getMsgSuccc() {
        MessageBody body = new MessageBody();
        setVal(true, body);
        return body;
    }

    public static MessageBody getMsgFail() {
        MessageBody body = new MessageBody();
        setVal(false, body);
        return body;
    }

    public static MessageBody getErrorMessageBody(String errorInfo) {
        MessageBody body = new MessageBody();
        setVal(false, body);
        body.setMsg(errorInfo);
        return body;
    }

    public static MessageBody getCostomMessageBody(String msg, boolean success) {
        MessageBody body = new MessageBody();
        setCustomVal(success, body);
        body.setMsg(msg);
        return body;
    }

    public static MessageBody getCostomMessageBody(String msg, boolean success, Object data) {
        MessageBody body = new MessageBody();
        setCustomVal(success, body);
        body.setMsg(msg);
        body.setData(data);
        return body;
    }


    public static MessageBody getErrorMessageBody(String errorInfo, int errorCode) {
        MessageBody body = new MessageBody();
        body.setSuccess(false);
        body.setMsg(errorInfo);
        body.setCode(errorCode);
        return body;
    }


    public static MessageBody getMessageBody(Object data) {
        MessageBody body = new MessageBody();
        setVal(true, body);
        body.setData(data);
        return body;
    }

    public static MessageBody getInsideError() {
        MessageBody body = new MessageBody();
        setVal(false, body);
        return body;
    }

    private static void setCustomVal(boolean success, MessageBody body) {
        body.setSuccess(success);
        if (success) {
            body.setCode(EnumError.SUCCESS.getCode());

        } else {
            body.setCode(EnumError.FAILED.getCode());

        }
    }

    private static void setVal(boolean success, MessageBody body) {
        body.setSuccess(success);
        if (success) {
            body.setCode(EnumError.SUCCESS.getCode());
            body.setMsg(EnumError.SUCCESS.getMsg());
        } else {
            body.setCode(EnumError.FAILED.getCode());
            body.setMsg(EnumError.FAILED.getMsg());
        }
    }
}
